package cn.iocoder.yudao.module.erp.controller.admin.accountplan;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.erp.controller.admin.accountplan.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.accountplan.AccountPlanDO;
import cn.iocoder.yudao.module.erp.service.accountplan.AccountPlanService;

@Tag(name = "管理后台 - 对账单对应计划数据")
@RestController
@RequestMapping("/erp/account-plan")
@Validated
public class AccountPlanController {

    @Resource
    private AccountPlanService accountPlanService;

    @PostMapping("/create")
    @Operation(summary = "创建对账单对应计划数据")
    @PreAuthorize("@ss.hasPermission('erp:account-plan:create')")
    public CommonResult<Long> createAccountPlan(@Valid @RequestBody AccountPlanSaveReqVO createReqVO) {
        return success(accountPlanService.createAccountPlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新对账单对应计划数据")
    @PreAuthorize("@ss.hasPermission('erp:account-plan:update')")
    public CommonResult<Boolean> updateAccountPlan(@Valid @RequestBody AccountPlanSaveReqVO updateReqVO) {
        accountPlanService.updateAccountPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除对账单对应计划数据")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:account-plan:delete')")
    public CommonResult<Boolean> deleteAccountPlan(@RequestParam("id") Long id) {
        accountPlanService.deleteAccountPlan(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得对账单对应计划数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:account-plan:query')")
    public CommonResult<AccountPlanRespVO> getAccountPlan(@RequestParam("id") Long id) {
        AccountPlanDO accountPlan = accountPlanService.getAccountPlan(id);
        return success(BeanUtils.toBean(accountPlan, AccountPlanRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得对账单对应计划数据分页")
    @PreAuthorize("@ss.hasPermission('erp:account-plan:query')")
    public CommonResult<PageResult<AccountPlanRespVO>> getAccountPlanPage(@Valid AccountPlanPageReqVO pageReqVO) {
        PageResult<AccountPlanDO> pageResult = accountPlanService.getAccountPlanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AccountPlanRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出对账单对应计划数据 Excel")
    @PreAuthorize("@ss.hasPermission('erp:account-plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAccountPlanExcel(@Valid AccountPlanPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AccountPlanDO> list = accountPlanService.getAccountPlanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "对账单对应计划数据.xls", "数据", AccountPlanRespVO.class,
                        BeanUtils.toBean(list, AccountPlanRespVO.class));
    }

}